package com.example.demo.controller;


import com.example.comon.domain.R;
import com.example.demo.domain.Bo.UserRoleBo;
import com.example.demo.domain.Role;
import com.example.demo.domain.UserRole;
import com.example.demo.service.IUserRoleService;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/userRole")
@Api(value = "userRole",tags = "用户角色控制器")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @PostMapping("/addUserRole")
    public R addUserRole(@RequestBody  UserRoleBo bo) {
        UserRole userRoleRes = userRoleService.getByUserIdAndRoleId(bo.getUserId(),bo.getRoleId());
        if(userRoleRes !=null){
                return R.error("该用户已授权该角色，请勿重复授权");
        }

        UserRole userRole = new UserRole();
        userRole.setRoleId(bo.getRoleId());
        userRole.setUserId(bo.getUserId());
        userRole.setCreateId(1111L);
        userRoleService.addUserRole(userRole);
        return R.success("授权成功");
    }

    /**
     * 查询用户的角色（用户有哪些角色）
     * @param userId
     * @return
     */
    @PostMapping("/getRoleByUserId")
    public R getRoleByUserId(Long userId) {
        List<Role>  list = userRoleService.getRoleByUserId(userId);
        return R.success( list);
    }

}
